package com.ruoyi.project.jiaqin.companymsg.controller;

import java.util.List;

import com.ruoyi.project.jiaqin.company.domain.Company;
import com.ruoyi.project.jiaqin.company.service.ICompanyService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.jiaqin.companymsg.domain.CompanyMsg;
import com.ruoyi.project.jiaqin.companymsg.service.ICompanyMsgService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 企业留言Controller
 *
 * @author admin
 * @date 2021-02-02
 */
@Controller
@RequestMapping("/jiaqin/companymsg")
public class CompanyMsgController extends BaseController
{
    private String prefix = "jiaqin/companymsg";

    @Autowired
    private ICompanyMsgService companyMsgService;
    @Autowired
    private ICompanyService iCompanyService;

    @RequiresPermissions("jiaqin:companymsg:view")
    @GetMapping()
    public String companymsg()
    {
        return prefix + "/companymsg";
    }

    /**
     * 查询企业留言列表
     */
    @RequiresPermissions("jiaqin:companymsg:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(CompanyMsg companyMsg)
    {
        Company company = iCompanyService.selectCompanyBySysUserId(getSysUser().getUserId());
        if(company != null) {
            companyMsg.setCompanyId(company.getId());
            startPage();
            List<CompanyMsg> list = companyMsgService.selectCompanyMsgList(companyMsg);
            return getDataTable(list);
        }
        if(getSysUser().isAdmin()){
            startPage();
            List<CompanyMsg> list = companyMsgService.selectCompanyMsgList(companyMsg);
            return getDataTable(list);
        }
        return new TableDataInfo();
    }

    /**
     * 导出企业留言列表
     */
    @RequiresPermissions("jiaqin:companymsg:export")
    @Log(title = "企业留言", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(CompanyMsg companyMsg)
    {
        List<CompanyMsg> list = companyMsgService.selectCompanyMsgList(companyMsg);
        ExcelUtil<CompanyMsg> util = new ExcelUtil<CompanyMsg>(CompanyMsg.class);
        return util.exportExcel(list, "companymsg");
    }

    /**
     * 新增企业留言
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存企业留言
     */
    @RequiresPermissions("jiaqin:companymsg:add")
    @Log(title = "企业留言", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(CompanyMsg companyMsg)
    {
        return toAjax(companyMsgService.insertCompanyMsg(companyMsg));
    }

    /**
     * 修改企业留言
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        CompanyMsg companyMsg = companyMsgService.selectCompanyMsgById(id);
        mmap.put("companyMsg", companyMsg);
        return prefix + "/edit";
    }

    /**
     * 修改保存企业留言
     */
    @RequiresPermissions("jiaqin:companymsg:edit")
    @Log(title = "企业留言", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(CompanyMsg companyMsg)
    {
        return toAjax(companyMsgService.updateCompanyMsg(companyMsg));
    }

    /**
     * 删除企业留言
     */
    @RequiresPermissions("jiaqin:companymsg:remove")
    @Log(title = "企业留言", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(companyMsgService.deleteCompanyMsgByIds(ids));
    }
}
